#include "MainMenuForm.h"
#include "NavigationManager.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Io;
using namespace Osp::Text;
using namespace Osp::Media;
using namespace Osp::Graphics;

result MainMenuForm::Construct() {
	Form::Construct(L"IDF_MENU");
	return E_SUCCESS;
}

result MainMenuForm::OnInitializing() {
	result r = E_SUCCESS;
	__pScrollPanel = static_cast<ScrollPanel *>(GetControl(L"IDC_SCROLLPANEL1", false));

	__pCustomListItemFormat = new CustomListItemFormat();
	__pCustomListItemFormat->Construct();
	__pCustomListItemFormat->AddElement(MENU_ICO_ID, Osp::Graphics::Rectangle(32, 43, 64, 64));
	__pCustomListItemFormat->AddElement(MENU_NAME_ID, Osp::Graphics::Rectangle(128, 50, 460, 50), 40);
	__pListMenu = static_cast<CustomList *>(GetControl(L"IDPC_LIST_MENU", true));
	Osp::Media::Image *pImage = new Osp::Media::Image();
	pImage->Construct();
	__pMap = pImage->DecodeN(L"/Res/map.png", BITMAP_PIXEL_FORMAT_ARGB8888);
	__pNavigateTo = pImage->DecodeN(L"/Res/navigate_to.png", BITMAP_PIXEL_FORMAT_ARGB8888);
	if (__pListMenu != null) {
		__pListMenu->AddCustomItemEventListener(*this);
		CustomListItem* pItem = new CustomListItem();
		pItem->Construct(150);
		pItem->SetItemFormat(*__pCustomListItemFormat);
		pItem->SetElement(MENU_ICO_ID, *__pNavigateTo, __pNavigateTo);
		pItem->SetElement(MENU_NAME_ID, L"Navigieren zu");
		__pListMenu->AddItem(*pItem, ID_NAVIGATE_TO);
		pItem = new CustomListItem();
		pItem->Construct(150);
		pItem->SetItemFormat(*__pCustomListItemFormat);
		pItem->SetElement(MENU_ICO_ID, *__pMap, __pMap);
		pItem->SetElement(MENU_NAME_ID, L"Karte");
		__pListMenu->AddItem(*pItem, ID_MAP);
	}
	return r;
}

result MainMenuForm::OnTerminating() {
	result r = E_SUCCESS;
	return r;
}

void MainMenuForm::OnItemStateChanged(const Osp::Ui::Control &source, int index, int itemId, Osp::Ui::ItemStatus status) {
	Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
	NavigationManager *pNavManager = static_cast<NavigationManager *>(pFrame->GetControl(NavigationManager::FORM_MANAGER_NAME));
	if (pNavManager != null) {
		switch(itemId) {
		case ID_NAVIGATE_TO:
			{
				pNavManager->SendUserEvent(NavigationManager::REQUEST_ID_ADDRESS_COUNTRY, null);
			}
			break;
		case ID_MAP:
			{
				pNavManager->SendUserEvent(NavigationManager::REQUEST_ID_MAP_FORM, null);
			}
			break;
		default:
			break;
		}
	}
}
